// 初始化组件配置
export const initFormConfig = (formItem: any) => {
  return {
    title: formItem.formTypeName,
    tips: '', // 提示语
    required: false, // 是否必填
    key: formItem.id, // 表单项key值

    columns: [],
    addRow: false,
    total: false,
    defaultRow: 3,
    maxRow: 50,
    exportVal: ''
  }
}

// 初始化子组件配置
export const initInputConfig = (key: string) => {
  return {
    key, // 字段key
    title: '', // 字段名
    required: true, // 必填校验
    type: '', // 字段类型
    fmtFn: '', // 数据格式化
    validateFn: '' // 数值校验
  }
}

// 子组件类型
export const inputTypeArr = [
  {
    label: '文字',
    value: 'string'
  },
  {
    label: '图像',
    value: 'image'
  },
  {
    label: '单行文本输入',
    value: 'TextInput'
  },
  {
    label: '多行文本输入',
    value: 'TextAreaInput'
  },
  {
    label: '数值输入',
    value: 'NumberInput'
  },
  {
    label: '日期选择',
    value: 'DateSelect'
  }
]

// 初始化table列表数据
export const initTableList = (formItemConfig: any) => {
  const list: any = []
  const defaultRow = formItemConfig.defaultRow
  const defaultObj = initTableObject(formItemConfig)
  for (let i = 0; i < defaultRow; i++) {
    const obj = JSON.parse(JSON.stringify(defaultObj))
    list.push(obj)
  }
  return list
}

// 初始化table列表数据项
export const initTableObject: (formItemConfig: any) => {
  [key: string]: any
} = (formItemConfig) => {
  const columns = formItemConfig.columns
  const defaultObj: { [key: string]: any } = {}
  for (let i = 0; i < columns.length; i++) {
    if (columns[i].type == 'string') {
      defaultObj[columns[i].key] = '${固定文本}'
    } else if (columns[i].type == 'image') {
      defaultObj[columns[i].key] = ''
    } else if (columns[i].type == 'NumberInput') {
      defaultObj[columns[i].key] = undefined
    } else {
      defaultObj[columns[i].key] = ''
    }
  }
  return defaultObj
}

// 校验数字范围
export const validateFormValue = (formValue: any, formConfig: any) => {
  const list = JSON.parse(formValue)
  for (let index = 0; index < formConfig.columns.length; index++) {
    const col = formConfig.columns[index]
    // 数值类型校验 正数校验
    if (col.type === 'NumberInput') {
      for (let i = 0; i < list.length; i++) {
        const row = list[i]
        const reg = new RegExp('^[0-9]*$')
        if (row[col.key] && !reg.test(row[col.key])) {
          return `第${i + 1}条数据${col.title}数值校验失败`
        }
      }
    }
    // 必填校验
    if (col.required) {
      for (let i = 0; i < list.length; i++) {
        const row = list[i]
        if (row[col.key] === '' || row[col.key] === null || row[col.key] === undefined) {
          return `第${i + 1}条数据${col.title}必填校验失败`
        }
      }
    }
    // 自定义校验
    if (col.validateFn) {
      const all = totalFn(list, col.type, col.key)
      for (let i = 0; i < list.length; i++) {
        const row = list[i]
        const val = row[col.key] || 0
        const fnStr = importValToFn(formConfig.columns, row, col.validateFn)
        const fn = new Function('all', 'val', fnStr)
        const valid = fn(all, val)
        let errorStr = importValToFn(formConfig.columns, row, col.validateFn)
        errorStr = formatErrorStr(col.title, errorStr)
        if (!valid) {
          return `${formConfig.title}分数校验失败，请核对是否填写正确`
        }
      }
    }
  }
}

export const importValToFn = (columns: any[], row: any, fnStr: string) => {
  for (let i = 0; i < columns.length; i++) {
    const col = columns[i]
    fnStr = fnStr.replace(col.key, row[col.key])
  }
  return fnStr
}

// 格式化数据
export const formatErrorStr = (title: string, errorStr: string) => {
  return errorStr
    .replace('all', `${title}合计`)
    .replace('val', `${title}数值`)
    .replace('&&', ` 且 `)
    .replace('return', ``)
}

// 合计数据输出
export const totalFn = (list: any[], type: string, key: string) => {
  if (type != 'NumberInput') return undefined
  const hasValue = list.every((el) => {
    return el[key] != '' && el[key] != undefined
  })
  if (!hasValue) return undefined
  if (list.length === 0) return undefined
  const arr = list.map((el) => {
    return el[key] ? Number(el[key]) : 0
  })
  return arr.reduce((a, b) => {
    return a + b
  }, 0)
}

// 合计数据输出
export const exportValFn = (list: any[], opt: any) => {
  if (opt.exportVal) {
    const countCol = opt.columns.filter((el: any) => {
      return opt.exportVal.indexOf(el.key) > -1
    })
    const data = countCol.map((el: any) => {
      return {
        key: el.key,
        value: totalFn(list, el.type, el.key)
      }
    })
    let exportVal = opt.exportVal
    for (let i = 0; i < data.length; i++) {
      // console.log(data[i].key, data[i].value)
      exportVal = exportVal.replace(data[i].key, data[i].value)
      // console.log(exportVal)
    }
    const fn = new Function(exportVal)
    return fn()
  } else {
    return undefined
  }
}
